﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Diagnostics;
using System.Reflection;

namespace TextFlow.Activities
{
	public abstract class ActionInputActivity : Activity
	{
        public ActionInputActivity()
        {
        }

        public ActionInputActivity( string name ) : base( name )
        {
        }

        public string BoundPropertyName { get; set; }
        public Expression LHS { get; set; }

        protected override ActivityExecutionStatus Execute( ActivityExecutionContext aec )
        {
            Debug.Assert( LHS != null );

            PropertyInfo prop = this.GetType().GetProperty( BoundPropertyName );

            Debug.Assert( prop != null );

            ServiceLookupDelegate svclookup = delegate( Type t )
            {
                if ( t == typeof( TextFlowWorkflow ) )
                {
                    return this.GetActivityByName( "MainFlow", false );
                }
                else
                {
                    return aec.GetService( t );
                }
            };

            object result = Utils.ComputeResult( LHS, svclookup );

            prop.SetValue( this, result, null );

            return ActivityExecutionStatus.Closed;
        }
    }
}
